import type {User} from "@/interface/User";
import { onMounted , ref , computed } from "vue";
import { useLoginStore } from  "@/stores/loginStore";
import { twinsApi , statisticsApi } from "@/controller/UserApi";
import { useRouter } from "vue-router"
import { hotSearchMusicApi } from "@/controller/ExploreApi"


export default function () {

    const loginStore = useLoginStore();

    let router = useRouter();

    let workerInfo = ref({} as User);

    onMounted(()=> {
        twins();
        if (loginStore.loginInfo.user) {
            workerInfo.value = loginStore.loginInfo.user.user;
        }
        statistics();
        hotSearchMusic();
    });

    let music = ref({} as any)
    function hotSearchMusic() {
        hotSearchMusicApi().then(res => {
            if (res.code === 200) {
                music.value = res.data;
                console.log(music.value);
            }
        })
    }



    let welcome : any  = computed(()=>{
        const now = new Date();
        const hour = now.getHours(); // 获取当前小时数（0-23）
        if (hour >= 0 && hour < 12) {
            return '上午好';
        } else if (hour >= 12 && hour < 18) {
            return '下午好';
        } else {
            return '晚上好';
        }
    });
    let twinsVal = ref<string>('');
    function twins() {
       twinsApi().then( res => {
            if (res.code === 200) {
                twinsVal.value = res.data;
            }
       });
    }

    let statisticsVal = ref({
        readCount: 0,
        waitAssign: 0,
        newToday: 0
    });
    function statistics() {
        if(workerInfo.value) {
            statisticsApi(workerInfo.value.id).then(res => {
                if (res.code === 200) {
                    statisticsVal.value = res.data;
                }
            });
        }
    }

    function logout() {
        loginStore.loginOut();
        router.push('/login');
    }

    function userMore() {
        router.push('/other/personalCenter');
    }

    return { workerInfo , welcome , twinsVal , statisticsVal , userMore , music , hotSearchMusic  }
}
